
import { Component, OnInit, ElementRef, AfterViewChecked, ViewChild } from '@angular/core';
import { DxDataGridComponent } from 'devextreme-angular';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { Resource } from '../../model/report/resource';
import { QueryModel } from '../../model/report/query-model';
import { ResponseModel } from '../../model/comm/response-model';
import { ChartOptionCreater } from '../../model/comm/chart-option-creater';
import { Shift } from '../../model/report/shift';
declare const $: any;
declare var swal: any;
import * as Enumerable from 'linq';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';
import { TipPtrSummary } from 'app/ptpmc/model/report/tip-ptr-summary';

@Component({
    selector: 'app-report',
    templateUrl: './tipnew.component.html',
    styleUrls: ['./tipnew.component.scss']
})

export class TipnewComponent implements OnInit,AfterViewChecked{
    title = 'Tipnew';
    public tipnewList: any[];
    /*生产线 - 班次*/
    public subAreaItems: SubArea[];

    public equipemntItems: TVResource[] = [];
    public subAareSelected: any;
    public equipmentSelected: TVResource[];

    @ViewChild('dddb') dddb: DxDropDownBoxComponent;
    clearEquipmentSelected() {
        this.equipmentSelected = [];
    }

    constructor(private commServices: CommService) {
    }

    ngOnInit() {

        this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD HH:mm');
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
        this.equipmentSelected = this.equipemntItems;
    }

    equipmentSelectedOk() {
        this.dddb.instance.close();
    }
    ngAfterViewChecked() {
        $('.table-box-body .dx-datagrid-text-content').css('text-align', 'center');

    }
    onSubAreaSelectedChange(subArea: any): void {
        this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);
        this.equipmentSelected = this.equipemntItems;
    }

    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        queryModel.queryResourceIds = this.equipmentSelected.map(e => e.resource);

        this.commServices
            .getList(queryModel, ReportUrls.TipnewList)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.tipnewList = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    getTime(id: any): String {
        const timerange: String = $('#' + id).val();
        return timerange;
    }
    onExporting(e) {
        e.fileName = e.fileName + new Date().toISOString().replace(/[\-\:\.]/g, '').split('T')[0];
    }
}
